import * as loginApi from '@/common/apis/login-api';
import * as relaNameApi from '@/common/apis/relaName-api';
import user from '@/store/modules/user.js';

export default {
    namespaced: true,
    state: {
        mmvEnv: "",
        isShowLogin: false,
        storeInfo: {
            contactAddress: "",
            contactTel: "",
            desc: "",
            distance: null,
            distanceDesc: "",
            storeId: 0,
            storeName: "选择门店",
        },
    },
    mutations: {
        setMmvEnv(state, mmvEnv) {
            state.mmvEnv = mmvEnv;
        },
        setStoreInfo(state, storeInfo) {
            state.storeInfo = storeInfo;
        },
        setIsShowLogin(state, isShowLogin) {
            state.isShowLogin = isShowLogin;
        },
    },
    actions: {
        async getStoreList({ commit }, { pageNo, pageSize }) {
            return new Promise(resolve => {
                uni.getLocation({
                    type: 'wgs84',
                    success: (({ latitude, longitude }) => {
                        loginApi.storelist({
                            pageNo: pageNo,
                            pageSize: pageSize,
                            longitude: longitude,
                            latitude: latitude,
                        }).then(res => {
                            this.storeList = res;
                            resolve(res);
                        }).catch(err => {
                            resolve([]);
                        });
                    }),
                });
            })
        },
        async getStoreInfo({dispatch, commit }, deptId) {
            let result;
            if (!uni.$u.test.isEmpty(deptId)) {
                result = await loginApi.storelist({ deptId });
            } else {
                result = await loginApi.store();
            }
			
			if(uni.$u.test.isEmpty(result)){
				//返回的门店是空，可能是本地缓存变脏数据，需要清除缓存，重新选择门店
				uni.setStorageSync('deptId', '');
				commit('user/savedeptId', '', { root: true });
				dispatch('getStoreInfo', '');
				// this.getStoreInfo('');
				
			}else{
				// #ifdef H5
				//h5环境直接取第一个门店
				commit('setStoreInfo', result[0]);
				commit('user/savedeptId', result[0].storeId, { root: true });
				setTimeout(()=>{
					uni.$emit('store_selected', true);
				}, 1000)
				// #endif
				
				// #ifndef H5
				// if(uni.$u.test.isEmpty(deptId) && user.state.deptChoseFlag){
				// 	//进入小程序没有门店id，应该是搜索进入，并且门店可以选择，要弹出选择门店框
				// 	// uni.$emit('store_wait_select', result[0]);
				// 	// uni.$emit('store_wait_select', result);
				// }else{
				// 	//有默认门店id，应该是扫码或者分享进入或者本地有缓存，不用弹框直接存门店信息
				// 	commit('setStoreInfo', result[0]);
				// 	commit('user/savedeptId', result[0].storeId, { root: true });
				// 	setTimeout(()=>{
				// 		//等待一下首页各个组件mounted
				// 		uni.$emit('store_selected', true);
				// 	}, 1000)
				// }
				// #endif
			}
            
        },
        /**
         * 实名验证
         */
        async verifyRelaName({ }, { openid, redirect }) {
            try {
                const result = await relaNameApi.valiAuthentication({ openid });
                if (result != 2) {
                    uni.navigateTo({
                        url: '/pages/real_name/real_name?redirect=' + encodeURIComponent(redirect),
                    });
                    return false;
                }
                return true;
            } catch (error) {
                uni.showToast({ title: error, icon: 'error' });
                return false;
            }
        },
    },
}